﻿using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Profiling;

// 为何不对渲染过程进行抽象 ，然后进行runtime Switch 渲染过程
public partial class CameraRender
{
//_________________________________  Field  ______________________________________________//

    ScriptableRenderContext context;
    Camera camera;
    CommandBuffer buffer = new CommandBuffer { name = bufferName };
    CullingResults cullingResults;
    bool isCull;

    static ShaderTagId[] UnSupportShaderTagIds = {
        new ShaderTagId("Always"),
        new ShaderTagId("ForwardBase"),
        new ShaderTagId("Vertex")
    };
    static ShaderTagId customShaderTagId = new ShaderTagId("CSRP Unlit");
    static Material errorMaterial; //Error Material 
    const string bufferName = "Render Camera";
#if UNITY_EDITOR
    string SampleName { get; set; }
#else
    const string SampleName = bufferName;
#endif

    //____________________         Function Declaretion   _________________________________//

    //Rendering Loop Function 
    public void Render(ScriptableRenderContext context, Camera camera)
    {
        this.context = context;
        this.camera = camera;
        PrepareSceneWindow();
        if (!Cull())
            return;
        Setup();
        DrawVisableGeometry();
        DrawUnsupportShaders();
#if UNITY_EDITOR
        DrawGizmos();
#endif
        DrawError();
        Submit();
    }
    bool Cull()
    {
        if (camera.TryGetCullingParameters(out ScriptableCullingParameters p))
        {
            cullingResults = context.Cull(ref p);
            return true;
        }
        return false;
    }
    void Setup()
    {
        context.SetupCameraProperties(camera);
        buffer.BeginSample(SampleName);
        CameraClearFlags flags = camera.clearFlags;
        buffer.ClearRenderTarget(
            flags < CameraClearFlags.Depth, 
            flags == CameraClearFlags.Color,
            flags == CameraClearFlags.Color ? camera.backgroundColor.linear : Color.black);
        ExcuteBuffer();
    }
    void ExcuteBuffer()
    {
        context.ExecuteCommandBuffer(buffer);
        buffer.Clear();
    }
    void Submit()
    {
        buffer.EndSample(SampleName);
        context.Submit();
    }
    //____________________ Partial Function Declaretion  _________________________________//

    partial void DrawVisableGeometry();
    partial void DrawUnsupportShaders();
    partial void DrawError();
    partial void DrawGizmos();
    partial void Preparebuffer();
    partial void PrepareSceneWindow();



}
